<?php
/*
 * Copyright (c) 2009 Tom Smith (tom@takeontom.com).
 * 
 * Distributed under the terms of the GNU Lesser General Public License.
 * 
 * ---------------------------------------------------------------------------------
 * 
 * This file is part of Seraph PHP Framework.
 *
 * Seraph PHP Framework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Seraph PHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Seraph PHP Framework.  If not, see <http://www.gnu.org/licenses/>.
*/


abstract class DBResult implements IteratorAggregate, ArrayAccess, Countable	{
	protected $result;
	
	protected $SQL;
	
	protected $errorMessage;
	protected $errorNumber;
	
	protected $DBConnection;
	
	protected $startTime;
	protected $endTime;
	protected $duration;
	
	protected $success = false;
	
	/**
	 * Requires the DBConnection object
	 * @param $DBConnection
	 */
	public function __construct(DBConnection $DBConnection)	{
		$this->DBConnection = $DBConnection;
	}
	
	/**
	 * Set the SQL used to generate this result
	 * @param $SQL
	 */
	public function setSQL($SQL)	{
		$this->SQL = $SQL;
	}
	/**
	 * Get the SQL used to generate this result
	 * @return string
	 */
	public function getSQL()	{
		return $this->SQL;
	}
	
	/**
	 * Get the database connection used for this result.
	 * 
	 * @return DBConnection
	 */
	public function getDBConnection()	{
		return $this->DBConnection;
	}
	
	/**
	 * Set the raw (db/driver/extension specific) result return value for this result.
	 * 
	 * @param $result
	 */
	public function setRawResult($result)	{
		$this->result = $result;
	}
	
	/**
	 * Get the raw result object for this result.
	 * @return variant
	 */
	public function getRawResult()	{
		return $this->result;
	}
	
	/**
	 * Get all the results as an associative array
	 * @return array
	 */
	abstract public function getResultArray();
	
	/**
	 * Get the number of rows in the result
	 * @return int
	 */
	abstract public function getRowCount();
	
	/**
	 * Get the number of rows affected by the last query.
	 * 
	 * @return int
	 */
	abstract public function getAffectedRowsCount();
	
	/**
	 * Get the number of fields in the result.
	 * @return int
	 */
	abstract public function getFieldCount();
	
	/**
	 * Get an array of the field names in the result
	 * @return array
	 */
	abstract public function getFields();
	
	/**
	 * Get the error number for the result.
	 * @return string
	 */
	public function getErrorNumber()	{
		return $this->errorNumber;
	}
	
	/**
	 * Get the error message for the result.
	 * @return string
	 */
	public function getErrorMessage()	{
		return $this->errorMessage;
	}
	
	/**
	 * Set the error number
	 * @param $errorNumber
	 */
	public function setErrorNumber($errorNumber)	{
		$this->errorNumber = $errorNumber;
	}
	
	/**
	 * Set the error message
	 * @param $errorMessage
	 */
	public function setErrorMessage($errorMessage)	{
		$this->errorMessage = $errorMessage;
	}
	
	/**
	 * Set that the query was a success.
	 * 
	 * @param $success
	 */
	public function setSuccess($success)	{
		$this->success = $success;
	}
	
	/**
	 * Whether the query was a success or not.
	 * 
	 * @return bool
	 */
	public function isSuccess()	{
		return !(isset($this->errorMessage));
	}
	
	/**
	 * Start the query timer
	 */
	public function start()	{
		unset($this->duration);
		$this->startTime = microtime(true);
	}
	/**
	 * Stop the query timer
	 */
	public function stop()	{
		$this->endTime = microtime(true);
	}
	/**
	 * Get the query duration.
	 * 
	 * start() and stop() need to have been executed prior to this 
	 * @return float
	 */
	public function getDuration()	{
		if (!isset($this->duration))	{
			$this->duration = $this->endTime - $this->startTime;
		}
		return $this->duration;
	}
	
	/**
	 * Get the last insert id
	 * @return int
	 */
	public function getLastInsertID()	{
		return $this->getDBConnection()->getLastInsertID();
	}
	

	/**
	 * From ArrayAccess interface.
	 * 
	 * Disabling setting of offset.
	 * 
	 * @param $offset
	 */
	public function offsetSet($offset, $value)	{
		return;
	}
	/**
	 * From ArrayAccess interface.
	 * 
	 * Disabling unsetting of offset.
	 * 
	 * @param $offset
	 */
	public function offsetUnset($offset)	{
		return;
	}
	
	/**
	 * From Countable interface.
	 * 
	 * @return int
	 */
	public function count()	{
		return $this->getRowCount();
	}
}
?>